package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseState;
import com.maoye.ossp.sys.base.service.SysBaseStateService;
@Controller
@RequestMapping(value = "/sys/base/state/")
public class SysBaseStateController extends BaseController {
	 @Resource 
	 private SysBaseStateService sysBaseStateService ;
	 
	 @RequestMapping(value = "getStateList")
		public String getStateList(HttpServletRequest request, Model model,
				SysBaseState state) {	
			QueryFilter queryFilter = getQuerFilter(request,state);
			Page<SysBaseState> page = (Page<SysBaseState>)sysBaseStateService
					.find(queryFilter);
			model.addAttribute("page", page);
			return resultPage("/sys/base/state/getStateList", model, request);
		}
		
		
		@RequestMapping(value = "addState")
		public String addState(HttpServletRequest request, Model model)
				throws IOException {
			Map map = new HashMap();
			SysBaseState sysBaseState = new SysBaseState();
			model.addAttribute("sysBaseState",sysBaseState);
			return "/sys/base/state/add";
		}
		/**
		 * 保存省州信息
		 * 
		 * @param request
		 * @param responser
		 * @throws IOException
		 */
		@RequestMapping(value = "saveState")
		public String saveState(SysBaseState state,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
			state.setId(idGenerator.getUId());
			state.setCode(state.getCode().replaceAll(",","").toString());
			//设置创建人
			state.setCreateBy("");
			//设置修改人
			state.setUpdateBy("");
			sysBaseStateService.saveState(state);
			addMessage(redirectAttributes, "保存成功");
			return "redirect:getStateList";
		}	
		@RequestMapping(value ="viewState")
		public String viewState(HttpServletRequest request, HttpServletResponse response,Model model){
			String code =request.getParameter("code");
			String countryName=request.getParameter("countryName");
			Map map = new HashMap();
			map.put("code",code);
			SysBaseState state=null;
			state=sysBaseStateService.getStatebyCode(map);
			model.addAttribute("state",state);
			return "/sys/base/state/view";
		}
		
		/**
		 * 检查省州编码
		 * 
		 * @param request
		 * @param responser
		 * @throws IOException
		 */
		@RequestMapping(value ="checkStateCode")
		public void checkStateCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
			Map map = new HashMap();
			String code = request.getParameter("code");	
			map.put("code",code);
			PrintWriter out = response.getWriter();
			JSONObject json = new JSONObject();
			if(sysBaseStateService.checkStateCode(map)){
				json.put("resCode",0); //可用
			}else{
				json.put("resCode",1);//不可以用
			}
			out.print(json.toString());
			out.flush();
			out.close();
		}
		
		@RequestMapping(value ="updateState")
		public String updateState(HttpServletRequest request, HttpServletResponse response,Model model){
			String code =request.getParameter("code");
			String countryName=request.getParameter("countryName");
			Map map = new HashMap();
			map.put("code",code);
			SysBaseState sysBaseState=null;
			sysBaseState=sysBaseStateService.getStatebyCode(map);
			model.addAttribute("sysBaseState",sysBaseState);
			model.addAttribute("countryName",countryName);
			return "/sys/base/state/update";
		}
		
		/**
		 * 修改省州信息
		 * 
		 * @param request
		 * @param responser
		 * @throws IOException
		 */
		@RequestMapping(value ="saveUpdateState")
		public String saveUpdateState(HttpServletRequest request,HttpServletResponse response,SysBaseState state,RedirectAttributes redirectAttributes){
			//设置创建人
			state.setCreateBy("");
			//设置修改人
			state.setUpdateBy("");
			sysBaseStateService.saveUpdateState(state);
			addMessage(redirectAttributes, "修改成功");
			return "redirect:getStateList";
		}
		/**
		 * 删除省州信息
		 * 
		 * @param request
		 * @param responser
		 * @throws IOException
		 */
		@RequestMapping(value ="removeState")
		public String removeState(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
			Map map = new HashMap();
			String code=request.getParameter("code");
			map.put("code",code);
			sysBaseStateService.removeState(map);
			addMessage(redirectAttributes, "删除成功");
			return "redirect:getStateList";
		}
}
